#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using pii = pair<int, int>;
#define rep(i, a, b) for (int i = (a); i <= (b); i++)
#define per(i, a, b) for (int i = (a); i >= (b); i--)
#define endl '\n'
const int N = 1e6 + 5;

int n, m;
int a[N];
void pushdown(int p) {
  while (p * 2 <= n) {
    int l = p << 1, r = p << 1 | 1;
    int mn = p;
    if (a[l] < a[mn]) mn = l;
    if (r <= n && a[r] < a[mn]) mn = r;
    if (mn == p) break;
    swap(a[p], a[mn]);
    p = mn;
  }
}
void pushup(int p) {
  while (p != 1 && a[p] < a[p >> 1]) {
    swap(a[p], a[p >> 1]);
    p >>= 1;
  }
}
void push(int x) {
  a[++n] = x;
  pushup(n);
}
void pop() {
  swap(a[1], a[n]);
  n--;
  pushdown(1);
}
int top() { return a[1]; }
int main() {
  ios::sync_with_stdio(0), cin.tie(0), cout.tie(0);
  cin >> m;
  while (m--) {
    int op, x;
    cin >> op;
    if (op == 1) {
      cin >> x;
      push(x);
    } else if (op == 2) {
      cout << top() << endl;
    } else {
      pop();
    }
  }
  return 0;
}